package com.medvalley.tcm.controller.admin.service;

import com.google.common.collect.Lists;
import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.base.Constants;
import com.medvalley.tcm.controller.admin.vo.inputvo.ManagerUserInputVo;
import com.medvalley.tcm.controller.admin.vo.outputvo.ManagerUserOutputVo;
import com.medvalley.tcm.controller.admin.vo.outputvo.PageDataOutputVo;
import com.medvalley.tcm.dao.ManagerDao;
import com.medvalley.tcm.domain.Manager;
import com.medvalley.tcm.utils.EncryptUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linsong
 * @date 2019/6/20 14:14
 **/

@Service
public class ManagerService {
    @Autowired
    private ManagerDao managerDao;

    public ManagerUserOutputVo createManager(ManagerUserInputVo managerUserInputVo) {
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerUserInputVo, manager);
        manager.setPassword(EncryptUtils.sha256(managerUserInputVo.getPassword()));
        managerDao.save(manager);
        ManagerUserOutputVo managerUserOutputVo = new ManagerUserOutputVo();
        BeanUtils.copyProperties(manager, managerUserOutputVo);
        return managerUserOutputVo;
    }

    public void delete(int id) {
        managerDao.deleteById(id);
    }

    public ManagerUserOutputVo login(String name, String password) {
        Manager manager = managerDao.findOneByName(name);
        if (manager == null) {
            throw new BusinessException("密码错误");
        }
        if (!manager.getPassword().equals(EncryptUtils.sha256(password))) {
            throw new BusinessException("密码错误");
        }
        ManagerUserOutputVo managerUserOutputVo = new ManagerUserOutputVo();
        BeanUtils.copyProperties(manager, managerUserOutputVo);
        return managerUserOutputVo;
    }

    public PageDataOutputVo<ManagerUserOutputVo> searchManagerUser(int pageNo, int pageSize) {
        PageDataOutputVo<ManagerUserOutputVo> pageDataOutputVo = new PageDataOutputVo();
        List<ManagerUserOutputVo> managerUserOutputVoList = Lists.newArrayList();
        pageDataOutputVo.setPageNo(pageNo);
        pageDataOutputVo.setPageSize(pageSize);
        pageNo = pageNo - 1 < 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.DESC, "id");
        Specification<Manager> specification = new Specification<Manager>() {
            @Override
            public Predicate toPredicate(Root<Manager> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(criteriaBuilder.gt(root.get("privilege").as(Integer.class), 0));
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
        Page<Manager> pageData = managerDao.findAll(specification, pageable);
        if (pageData.hasContent()) {
            List<Manager> managerList = pageData.getContent();
            for (Manager m : managerList) {
                ManagerUserOutputVo managerUserOutputVo = new ManagerUserOutputVo();
                managerUserOutputVo.setId(m.getId());
                managerUserOutputVo.setName(m.getName());
                managerUserOutputVo.setPhone(m.getPhone());
                managerUserOutputVo.setCreateTime(DateFormatUtils.format(m.getCreateTime(), Constants.YYYY_MM_DD_HH_MM_SS));
                managerUserOutputVoList.add(managerUserOutputVo);
            }
        }
        pageDataOutputVo.setTotalPageCount(pageData.getTotalPages());
        pageDataOutputVo.setTotalElement(pageData.getTotalElements());
        pageDataOutputVo.setPageData(managerUserOutputVoList);
        return pageDataOutputVo;
    }
}
